//
//  ViewController.m
//  GCD常用函数
//
//  Created by 李煜 on 2024/3/20.
//

#import "ViewController.h"
@interface ViewController ()

@end

@implementation ViewController

-(void)viewDidLoad {
  [super viewDidLoad];
  
//   [self invocationOperation];
//   [self blockOperationNoExecution];
   [self blockOperationExecution];


   //  NSOperation是抽象类,必须使用其子类: NSBlockOperation 和  NSInvocationOperation.
}
/**
 NSBlockOperation多了两个方法,一个属性,如下
 + (instancetype)blockOperationWithBlock:(void (^)(void))block;

 - (void)addExecutionBlock:(void (^)(void))block;
 @property (readonly, copy) NSArray<void (^)(void)> *executionBlocks;

 */

//NSBlockOperation有Execution
- (void)blockOperationExecution {

   NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
      NSLog(@"--1--%@",[NSThread currentThread]);
   }];
   NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
      NSLog(@"--2--%@",[NSThread currentThread]);
   }];
   NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
      NSLog(@"--3--%@",[NSThread currentThread]);
   }];
   //指定的块不应该对其执行环境做任何假设:此方式主线程子线程都可能执行,不确定
   //注意:如果一个操作中的任务数量>1,那么会开子线程并发执行任务,有可能主线程
   [op3 addExecutionBlock:^{
      NSLog(@"--4--%@",[NSThread currentThread]);
   }];
   [op3 addExecutionBlock:^{
      NSLog(@"--5--%@",[NSThread currentThread]);
   }];
   [op3 addExecutionBlock:^{
      NSLog(@"--6--%@",[NSThread currentThread]);
   }];

   [op1 start];
   [op2 start];
   [op3 start];
   /**
    结果是op3中的所有任务有可能开启新线程
    2024-03-22 11:07:02.100490+0800 NSOperation基本概念[28960:406041] --1--<_NSMainThread: 0x600003da4980>{number = 1, name = main}
    2024-03-22 11:07:02.103001+0800 NSOperation基本概念[28960:406041] --2--<_NSMainThread: 0x600003da4980>{number = 1, name = main}
    2024-03-22 11:07:02.107117+0800 NSOperation基本概念[28960:406151] --3--<NSThread: 0x600003df4240>{number = 7, name = (null)}
    2024-03-22 11:07:02.107110+0800 NSOperation基本概念[28960:406150] --4--<NSThread: 0x600003de8680>{number = 5, name = (null)}
    2024-03-22 11:07:02.107110+0800 NSOperation基本概念[28960:406149] --5--<NSThread: 0x600003df9800>{number = 8, name = (null)}
    2024-03-22 11:07:02.107364+0800 NSOperation基本概念[28960:406041] --6--<_NSMainThread: 0x600003da4980>{number = 1, name = main}

    */
}
//无execution
- (void)blockOperationNoExecution {//没啥卵用,跟直接调用[self download1]没区别

   NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
      NSLog(@"--1--%@",[NSThread currentThread]);
   }];
   NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
      NSLog(@"--2--%@",[NSThread currentThread]);
   }];
   NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
      NSLog(@"--3--%@",[NSThread currentThread]);
   }];

   [op1 start];
   [op2 start];
   [op3 start];

}
- (void)invocationOperation {//没啥卵用,跟直接调用[self download1]没区别
   NSInvocationOperation *op1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(downlaod1) object:nil];
   [op1 start];
}
- (void)downlaod1 {
   NSLog(@"%s",__func__);
}

@end
